import json
 
import paho.mqtt.client as mqtt
 
"""rc 值	
0	连接成功
1	协议版本错误
2	无效的客户端标识
3	服务器无法使用
4	错误的用户名或密码
5	未经授权
"""
test=0
test_username=''
test_passwd=''
test_broker='127.0.0.1'
test_port = 9999
broker = '127.0.0.1'
port = 1883
username = 'shhooserver001'
passwd = 'shhoo2018'
if test is True:
    broker =test_broker
    port = test_port
    username = test_username
    passwd = test_passwd
topic = "E00120220718171201/PF/PP01/set_process"

def on_connect(client, userdata, flags, rc):
    return rc


def on_message(client, userdata, msg):
    data = msg.payload
    print(json.loads(data))


def main():
    client = mqtt.Client()
    client.username_pw_set(username=username, password=passwd)
    client.on_connect = on_connect
    client.on_message = on_message
    rc = client.connect(host=broker, port=port, keepalive=60)
    print('rc', rc)
    if rc == 0:
        client.subscribe(topic)
    else:
        print('连接失败')
        raise Exception
 
    # 无限循环收发消息
    client.loop_forever()
 
 
if __name__ == '__main__':
    main()